

import requests

class Request(object):
    def __init__(self, remote_computer_name=''):
        self.base_url = "https://miniprogram.meijiebao.org.cn/hscommunity/api"
        self.proxies = {}
        # self.proxies = {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'}
        self.remote_computer_name = remote_computer_name


    def __get(self, url, data={}):
        while True:
            try:
                ret = requests.get(url, data, timeout=2)
                return ret
            except Exception:
                pass

    def __post(self, url, data):
        while True:
            try:
                ret = requests.post(url, data, timeout=2)
                return ret
            except Exception:
                pass

    def init(self, name):
        url = self.base_url + "/ppxhs/machine/init"
        data = {
            'machine_num': name
        }
        print('url -->', url)
        ret = self.__post(url, data)
        print("ret.json() -->", ret.json())

    # 检测是否登录小红书
    def check_login_xhs(self, name, remote_computer_name=''):
        url = self.base_url + "/ppxhs/machine/check-login-xhs"
        print('url -->', url)
        data = {
            'machine_num': name,
            'remote_computer_name': remote_computer_name,
        }
        ret = self.__get(url, data)

        print("check_login_xhs -->ret.json() -->", ret.json())
        # input("<><<<")
        if ret.status_code == 200 and ret.json()['code'] == 200:
            return True
        else:
            return False

    # 获取微博账号密码
    def get_wb_user(self, name):
        url = self.base_url + "/ppxhs/machine/get-wb-user"
        data = {
            'machine_num': name
        }

        ret = self.__get(url, data)
        print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            username = ret.json()['data']['wb_username']
            password = ret.json()['data']['wb_pwd']
            return [username, password]
        else:
            print(ret.json()['msg'])
            # exit()

    # 更新博主id
    def update_user_info(self, data):
        """
        :param data:    {'machine_num': 1, 'blogger_id': 1, 'blogger_name': 1, 'blogger_img': ''}
        :return:
        """
        url = self.base_url + "/ppxhs/machine/update-user-info"
        print('data -->', data)
        ret = self.__post( url, data)
        print("ret.json() -->", ret.json())

    def delete_machine(self, machine_name):
        result_data = {
            'is_delete': 1,
            'machine_num': machine_name
        }
        self.update_user_info(result_data)


    # 获取发私信任务
    def get_message_task(self, name):
        url = self.base_url + "/ppxhs/message/get-task"
        data = {
            'machine_num': name
        }

        ret = self.__get(url, data)
        # print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 完成发私信任务
    def post_message_task(self, task_id):
        url = self.base_url + "/ppxhs/message/update-task"
        data = {
            'task_id': task_id
        }
        ret = self.__post(url, data)
        print("ret.json() -->", ret.json())

    # 获取发私信任务
    def get_blogger_list(self, name):
        url = self.base_url + "/ppxhs/message/get-blogger-list"
        data = {
            'machine_num': name
        }

        ret = self.__get(url, data)
        # print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 获取发私信任务
    def rsync_msg(self, data):
        url = self.base_url + "/ppxhs/message/rsync-msg"
        # data = {
        #     'machine_num': name
        # }
        ret = self.__post(url, data)
        print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 记录设备操作,避免重复操作
    def record_log(self, name):
        url = self.base_url + "/ppxhs/machine/record-log"
        data = {
            'machine_num': name
        }

        ret = self.__post(url, data)
        print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']
        # get-record

    # 查询设备10分钟内是否操作过
    def get_record(self, name):
        url = self.base_url + "/ppxhs/machine/get-record"
        data = {
            'machine_num': name
        }
        ret = self.__get(url, data)
        print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 查询是否有优先安排的任务
    def get_task_status(self, name):
        url = self.base_url + "/ppxhs/message/get-task-status"
        data = {
            'machine_num': name
        }
        ret = self.__get(url, data)
        print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()

    # 获取已开通设备列表
    def get_machine_list(self):
        url = self.base_url + "/ppxhs/machine/get-machine-list"
        data = {
            'remote_computer_name': self.remote_computer_name
        }

        ret = self.__get(url, data)
        print("ret.json() -->", ret.json())
        # input("<><<<")
        if ret.json()['code'] == 200:
            return ret.json()['data']


    # 获取当前ip
    def get_current_ip(self):
        url = "http://websiteaccount.bjhzkq.com/api/getIpaddr"
        while True:
            try:
                ret = self.__get(url)

                break
            except Exception:
                pass

        return ret.text

    #
    # 检测是否登录小红书
    def check_phone_exit(self, phone):
        url = self.base_url + "/ppxhs/machine/check-phone-exit"
        print('url -->', url)
        data = {
            'phone': phone
        }
        ret = self.__get(url, data)

        print("check_login_xhs -->ret.json() -->", ret.json())
        # input("<><<<")
        if ret.status_code == 200 and ret.json()['code'] == 200 and ret.json()['data']['flag']:
            return True
        else:
            return False


class RequestNew(Request):

    def __get(self, url, data={}):
        while True:
            try:
                ret = requests.get(url, data, timeout=2)
                return ret
            except Exception:
                pass

    def __post(self, url, data):
        while True:
            try:
                ret = requests.post(url, data, timeout=2)
                return ret
            except Exception:
                pass

    # 获取已开通设备列表
    def get_machine_list(self):
        url = self.base_url + "/send-msg-machine/get-machine-list"
        # print('url -->', url)
        data = {
            'length': 0,
            'remote_computer_name': self.remote_computer_name
        }

        ret = self.__get(url, data)
        # print("ret.json() -->", ret.json())
        # input("<><<<")
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 检测是否登录小红书
    def check_login_xhs(self, name, remote_computer_name=''):
        url = self.base_url + "/send-msg-machine/check-login-xhs"
        # print('url -->', url)
        data = {
            'machine_num': name,
            'remote_computer_name': remote_computer_name,
        }
        ret = self.__get(url, data)

        # print("check_login_xhs -->ret.json() -->", ret.json())
        # input("<><<<")
        if ret.status_code == 200 and ret.json()['code'] == 200:
            return True
        else:
            return False

    # 获取发私信任务
    def get_blogger_list(self, name):
        url = self.base_url + "/send-msg-message/get-blogger-list"
        data = {
            'machine_num': name
        }

        ret = self.__get(url, data)
        # print("url -->", url)
        # print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 获取发私信任务
    def get_message_task(self, name):
        url = self.base_url + "/send-msg-message/get-task"
        data = {
            'machine_num': name
        }

        ret = self.__get(url, data)
        # print("url -->", url)
        # print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']

    # 完成发私信任务
    def post_message_task(self, task_id):
        url = self.base_url + "/send-msg-message/update-task"
        data = {
            'task_id': task_id
        }
        ret = self.__post(url, data)
        # print("ret.json() -->", ret.json())

    def delete_machine(self, machine_name):
        result_data = {
            'is_delete': 1,
            'machine_num': machine_name
        }
        self.update_user_info(result_data)

    # 更新博主id
    def update_user_info(self, data):
        """
        :param data:    {'machine_num': 1, 'blogger_id': 1, 'blogger_name': 1, 'blogger_img': ''}
        :return:
        """
        url = self.base_url + "/send-msg-machine/update-user-info"
        # print('data -->', data)
        ret = self.__post( url, data)
        # print("ret.json() -->", ret.json())

    # 获取发私信任务
    def rsync_msg(self, data):
        url = self.base_url + "/send-msg-message/rsync-msg"
        # data = {
        #     'machine_num': name
        # }
        ret = self.__post(url, data)
        # print("ret.json() -->", ret.json())
        if ret.json()['code'] == 200:
            return ret.json()['data']